package org.example.dingtalkintegrationservice.dingtalk.dingback
import com.alibaba.fastjson.JSONObject
import okhttp3.*
import org.example.dingtalkintegrationservice.pojo.DingBack
import org.springframework.stereotype.Component

@Component
class HandleDingBack {
    static def sendPLMTaskResult(taskId, user, isPass, msg){
        def client = new OkHttpClient()
        def jsonObject = new JSONObject()
        jsonObject.put("taskid", taskId)
        jsonObject.put("user",user)
        jsonObject.put("ispass", isPass)
        jsonObject.put("msg", msg)
        def jsonString = jsonObject.toJSONString()
        def requestBody = RequestBody.create( MediaType.get("application/json; charset=utf-8"),jsonString)
        def request = new Request.Builder()
                .url("http://192.168.1.18:9090/zxlinkapi/flow/flows/dealflowpass")
//            .url("http://192.168.1.88:8082/callbacktest")
                .post(requestBody)
                .build()
        Response response = client.newCall(request).execute()
        print(response)
        if (response.isSuccessful()) {
            response.body().string()
        } else {
            response.message()
        }
    }
    static def receiveDingResult(content) {
        def pairs = content.split("&")
        def decodedMap = [:]
        pairs.each { pair ->
            def (key, value) = pair.split("=", 2)
            decodedMap[key] = URLDecoder.decode(value, "UTF-8")
        }
        def activityId = decodedMap["activityId"]
        def operator = decodedMap["operator"]
        def outResult = decodedMap["outResult"]
        def remark = decodedMap["remark"]
        def res=outResult == "agree" ? "1" : "0"
        sendPLMTaskResult(activityId, operator, res, remark)
        return [activityId,operator,res]
    }

    static void main(String[] args) {
        def encodedString = "outResult=agree&processInstanceId=q3yhrv7gTUSFoiwJ59n32w09661734252953&activityId=T0000239439&corpId=dingdd7e3fc5e5f67a9235c2f4657eb6378f&data=%5B%5D&remark=%E5%90%8C%E6%84%8F&title=%E7%AD%BE%E6%94%B6%E5%BC%80%E7%A5%A8&operator=292634471126036472&taskId=90817871583"
        print(receiveDingResult(encodedString))
    }
    DingBack exec(String input){
//    def flowFile = session.get()
//    def attributes = flowFile.getAttributes()
//    String input = flowFile.read().getText("UTF-8")

//    String activityId = flowFile.getAttribute("http.query.param.activityId")
//    String operator = flowFile.getAttribute("http.query.param.operator")
//    String outResult = flowFile.getAttribute("http.query.param.outResult")
//    String remark = flowFile.getAttribute("http.query.param.remark")
        def (activityId,operator,res)= receiveDingResult(input)
//    sendPLMTaskResult(activityId, operator, outResult == "agree" ? "1" : "0", remark)
    DingBack dingBack=new DingBack()
    dingBack.ext=input
    dingBack.activityId=activityId
    dingBack.operator=operator
    dingBack.res=res
    return dingBack
//        flowFile.ext=input
//        flowFile.currentScript="HandleDingCallBack"
//        flowFile.activityId=activityId
//        flowFile.operator=operator
//        flowFile.res=res
//        session.transfer(flowFile, REL_SUCCESS)
}
}